package com.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BaseEntity
{   
    public static String toString(Object ob)
    {
        //得到类中的成员变量
        Field[] fields = ob.getClass().getDeclaredFields();
        StringBuffer strBuf = new StringBuffer("[");
        for(int i=0; i<fields.length; i++)
        {
           //成员变量名称
            String fieldName = fields[i].getName();
            //拼接出方法名：getXxx()
            String methodName = "get" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
           if(methodName.equals("getSerialVersionUID")){continue;}
            try
            {
                //得到方法
                Method method = ob.getClass().getMethod(methodName, new Class[]{});
                //调用方法
                Object value = method.invoke(ob, new Object[]{});
                strBuf.append(fieldName + ":");
                strBuf.append(value + "  ");
            }
            catch (SecurityException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (NoSuchMethodException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IllegalArgumentException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (InvocationTargetException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        strBuf.append("]");
        return strBuf.toString();
    }
}

